//
//  UserData.m
//  HW-3
//
//  Created by tql on 2020/12/9.
//  Copyright © 2020 tql. All rights reserved.
//

#import "UserData.h"

static UserData *userdata = nil;
static NSString * infoURL;

@implementation UserData

+ (UserData*)shareUser{
    static dispatch_once_t once;
    dispatch_once(&once, ^{
        userdata = [[UserData alloc]init];
    });
    return userdata;
}

- (instancetype)initWithData:(NSDictionary *)data{
    self = [super init];
    self.Name = data[@"name"];
    self.Email = data[@"email"];
    self.Level = data[@"level"];
    self.Phone = data[@"phone"];
    return self;
}

+(void)LoadData:(NSDictionary *)data{
    UserData* user = [UserData shareUser];
    user.Name = data[@"name"];
    user.Email = data[@"email"];
    user.Level = data[@"level"];
    user.Phone = data[@"phone"];
}

+ (void) UpdateData{
//    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//    manager.requestSerializer = [AFJSONRequestSerializer serializer];
//    manager.responseSerializer = [AFJSONResponseSerializer serializer];
//    NSString *selfURL = @"http://172.18.176.202:3333/hw3/getinfo?name=MOSAD";
//    [manager GET:selfURL parameters:nil headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable response) {
//        NSDictionary *userData = (NSDictionary *)response;
//        [UserData LoadData:userData];
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        NSLog(@"Update failed");
//    }];
    
    // dictionary To NSJoson
    NSError *err = nil;
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"MOSAD", @"name", nil];
    NSData *jasonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&err];
    NSString *jason = [[NSString alloc]initWithData:jasonData encoding:NSUTF8StringEncoding];

    // 个人信息获取的接口路径
    infoURL = @"http://172.18.176.202:3333/hw3/getinfo";
    infoURL = [infoURL stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    NSURL *URL=[NSURL URLWithString:infoURL];

    // 建立一个带协议缓存类型的请求
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL cachePolicy:0 timeoutInterval:2.0f];
    NSString *contentType = @"application/json";
    [request setValue:contentType forHTTPHeaderField:@"Content-Type"];

    // 设置表单提交的方法
    [request setHTTPMethod:@"get"];

    // 设置要提交的参数
    [request setHTTPBody:[jason dataUsingEncoding:NSUTF8StringEncoding]];

    // 获取返回值
    NSData *recvData =[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *jsonString = [[NSString alloc] initWithData:recvData encoding:NSUTF8StringEncoding];
    NSLog(@"%@", jsonString);
    NSError *error = nil;
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:recvData options:NSJSONReadingAllowFragments error:&error];
   
    NSDictionary *userData = dict;
    [UserData LoadData:userData];
    
    
}

@end
